﻿using UnityEngine;
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFramework;
using GameFramework.DataTable;

namespace MYSTARFORCE
{
    public class ProcedureMain : ProcedureBase
    {
        // 计时
        private float m_ElapseSeconds = 0f;

        private ScrollBg scrollBg = null;

        protected override void OnEnter(IFsm<IProcedureManager> procedureOwner)
        {
            base.OnEnter(procedureOwner);
            GameEntry.Entity.ShowMyAircraft(new MyAircraftData(GameEntry.Entity.GenerateSerialId(), 10000) {
                Name = "My Aircraft"
            });

            scrollBg = Object.FindObjectOfType<ScrollBg>();
            if (scrollBg == null)
            {
                Log.Warning("Can not find ScrollBg");
                return;
            }

        }

        protected override void OnUpdate(IFsm<IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            m_ElapseSeconds += elapseSeconds;

            if (m_ElapseSeconds >= 1f)
            {
                m_ElapseSeconds = 0f;
                // 获取小行星数据表
                IDataTable<DRAsteroid> dtAsteroids = GameEntry.DataTable.GetDataTable<DRAsteroid>();
                // 产生新的坐标，该坐标位于EnemySpawnBoundary盒子里面
                float randomPosX = scrollBg.EnemySpawnBoundary.bounds.min.x + scrollBg.EnemySpawnBoundary.bounds.size.x * (float)Utility.Random.GetRandomDouble();
                float randomPosZ = scrollBg.EnemySpawnBoundary.bounds.min.z + scrollBg.EnemySpawnBoundary.bounds.size.z * (float)Utility.Random.GetRandomDouble();
                Vector3 pos = new Vector3(randomPosX, 0, randomPosZ);
                // 要生成的小行星的资源
                int id = 60000 + Utility.Random.GetRandom(dtAsteroids.Count);
                // 小行星的数据
                AsteroidData asData = new AsteroidData(GameEntry.Entity.GenerateSerialId(), id) 
                {
                    Position = pos
                };
                // 显示它
                GameEntry.Entity.ShowAsteroid(asData);

            }
        }
    }
}